<?php

class SearchGateway extends TableDataGateway
{           
        
    public function __construct()
    {
        $this->establishConnection();
    }
        
    public function __destruct(){}
    
    public function getDataTablesByTagId($tag_id)
    {
        $stmt = $this->database_connection_->newStatement('SELECT dt_id, dt_titel, g_name, e_name, e_abbr, t_name, dt_time, u_name as dt_user, tag_name, dt_verified_data, parent as globaltopic FROM data_table_view where tag_id=:tag_id order by dt_titel, e_name');
        $stmt->bindValue(':tag_id', $tag_id, PDO::PARAM_INT);
        $stmt->execute();
        $arr_return = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $arr_return[] = $row;
        return $arr_return;
    }
    
    public function getSearchResult($arr_s_values)
    {
        $arr_condition=array();
        if(strlen($arr_s_values['title']) > 0)
            $arr_condition[] = 'lower(dt_titel) like lower(:title)';
        if($arr_s_values['tag_id'] > 0)
            $arr_condition[] = 'tag_id = :tag_id';
        if($arr_s_values['layer_id'] > 0)
            $arr_condition[] = 'e_id = :layer_id';
        if($arr_s_values['region_id'] > 0)
            $arr_condition[] = 'g_id = :region_id';
        if($arr_s_values['globaltopic'] > 0)
            $arr_condition[] = 'tag_parent_id = :parent_id';
        $statement = "SELECT dt_id, dt_titel, g_name, e_name, e_abbr, t_name, dt_time, u_name as dt_user, tag_name, dt_verified_data, parent as globaltopic FROM data_table_view";
        if(count($arr_condition) > 0)
            $statement .= " where " . implode(' and ', $arr_condition);
        $statement .= " order by dt_titel, e_name";
        $stmt = $this->database_connection_->newStatement($statement);
        if(strlen($arr_s_values['title']) > 0)
            $stmt->bindValue(':title', '%'.$arr_s_values['title'].'%');
        if($arr_s_values['tag_id'] > 0)
            $stmt->bindValue(':tag_id', $arr_s_values['tag_id'], PDO::PARAM_INT);
        if($arr_s_values['layer_id'] > 0)
            $stmt->bindValue(':layer_id', $arr_s_values['layer_id'], PDO::PARAM_INT);
        if($arr_s_values['region_id'] > 0)
            $stmt->bindValue(':region_id', $arr_s_values['region_id'], PDO::PARAM_INT);
        if($arr_s_values['globaltopic'] > 0)
            $stmt->bindValue(':parent_id', $arr_s_values['globaltopic'], PDO::PARAM_INT);
        $stmt->execute();
        $arr_return = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $arr_return[] = $row;
        return $arr_return;
    }
}

?>
